//
//  LXCustomViewController.m
//  喜马拉雅改良版框架
//
//  Created by liuxin on 15/12/22.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "LXCustomViewController.h"
#import "LXCareViewController.h"
#import "LXSaveViewController.h"
#import "LXHistoryViewController.h"
@interface LXCustomViewController ()<UIScrollViewDelegate>
@property(nonatomic,strong)UIView *headView;
@property(nonatomic,strong)UIScrollView *myScrollView;
@property(nonatomic,strong)UIView *lineView;
@end

@implementation LXCustomViewController
-(void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBar.hidden = YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self createHeadView];
    [self createScrollView];
}
-(void)createScrollView{
    LXCareViewController *care = [LXCareViewController new];
    care.view.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-65);
    LXSaveViewController *save = [LXSaveViewController new];
    save.view.frame = CGRectMake(SCREEN_WIDTH, 0, SCREEN_WIDTH, SCREEN_HEIGHT-65);
    LXHistoryViewController *history = [LXHistoryViewController new];
    history.view.frame = CGRectMake(2*SCREEN_WIDTH, 0, SCREEN_WIDTH, SCREEN_HEIGHT-65);
    self.myScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 65, SCREEN_WIDTH, SCREEN_HEIGHT - 65)];
    self.myScrollView.pagingEnabled = YES;
    self.myScrollView.delegate = self;
    [self.view addSubview:self.myScrollView];
    self.myScrollView.contentSize = CGSizeMake(SCREEN_WIDTH*3, SCREEN_HEIGHT-65);

    [self addChildViewController:care];
    [self.myScrollView addSubview:care.view];
    [self addChildViewController:save];
    [self.myScrollView addSubview:save.view];
    [self addChildViewController:history];
    [self.myScrollView addSubview:history.view];
}
-(void)createHeadView{
    self.headView = [[UIView alloc]init];
    self.headView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.headView];
    [self.headView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.left.top.mas_equalTo(0);
        make.height.mas_equalTo(62);
    }];
    NSArray *array = @[@"关注",@"收藏",@"历史"];
    for (int i = 0; i < array.count; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(SCREEN_WIDTH/3*i, 15, SCREEN_WIDTH/3, 50);
        btn.backgroundColor = [UIColor whiteColor];
        [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        btn.tag = 1000+i;
        [btn addTarget:self action:@selector(careBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        [btn setTitle:array[i] forState:UIControlStateNormal];
        [self.headView addSubview:btn];
    }
    UIButton *temp =  [self.headView viewWithTag:1000];
    [temp setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    self.lineView = [[UIView alloc]init];
    self.lineView.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:self.lineView];
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.top.equalTo(self.headView.mas_bottom);
        make.width.mas_equalTo(SCREEN_WIDTH/3);
        make.height.mas_equalTo(3);
    }];
}
-(void)careBtnAction:(UIButton*)btn{
    for (int i = 0 ; i < 3; i++) {
        UIButton *temp =  [self.headView viewWithTag:1000+i];
        [temp setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    }
    [btn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    NSInteger num = btn.tag%10;
    self.myScrollView.contentOffset =CGPointMake((SCREEN_WIDTH*num), 0) ;
    [self.lineView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(SCREEN_WIDTH/3*num);
    }];
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    [self.lineView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(scrollView.contentOffset.x/3);
    }];
    for (int i = 0 ; i < 3; i++) {
        UIButton *temp =  [self.headView viewWithTag:1000+i];
        [temp setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    }
    UIButton *btn = [self.headView viewWithTag:1000+scrollView.contentOffset.x/SCREEN_WIDTH];
    [btn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
}

-(void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
@end
